﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using br.com.SistemaChamados.Model.Common;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.DAO.Common;

namespace br.com.SistemaChamados.Controller.Common
{
    public class UserController
    {

        public EnumGenericState InsertUser(User user)
        {
            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            try
            {
                stateTransaction = UserDAO.InsertUser(user);
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            return stateTransaction;
        }

        public EnumGenericState UpdateUser(User user)
        {
            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            try
            {
                stateTransaction = UserDAO.UpdateUser(user);
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            return stateTransaction;
        }

        public EnumGenericState DeleteUser(User user)
        {
            EnumGenericState stateTransaction = EnumGenericState.GENERIC_STATE_UNKNOW;
            try
            {
                stateTransaction = UserDAO.DeleteUser(user);
            }
            catch (Exception ex)
            {
                stateTransaction = EnumGenericState.GENERIC_STATE_FAILED;
                throw ex;
            }
            return stateTransaction;
        }

        public User GetUser(User user)
        {
            User ValidateUserSelected = new User();
            try
            {
                ValidateUserSelected = UserDAO.getUser(user);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ValidateUserSelected;
        }

        public List<User> GetUsers()
        {
            List<User> Returnedlist = new List<User>();

            try
            {
                Returnedlist = UserDAO.getUsers();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Returnedlist;
        }

        public EnumUserState validateUser(ref User user)
        {
            EnumUserState resultState = EnumUserState.USER_STATE_UNKNOW;
            try
            {
                user = UserDAO.getUser(user);
                if (user.Id == 0)
                {
                    resultState = EnumUserState.USER_STATE_INVALID; 
                }
                else
                {
                    resultState = EnumUserState.USER_STATE_LOGGED_IN;
                }                                
            }
            catch (Exception ex)
            {
                resultState = EnumUserState.USER_STATE_UNKNOW;
                throw ex;
            }
            return resultState;
        }

    }
}